<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="baidu-site-verification" content="zD47G9xe42" />


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="MySQL巩固与加强," />





  <link rel="alternate" href="/atom.xml" title="uiste" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="SQL DML 和 DDL可以把 SQL 分为两个部分：数据操作语言 (DML) 和 数据定义语言 (DDL)。SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。查询和更新指令构成了 SQL 的 DML 部分：SELECT - 从数据库表中获取数据UPDATE - 更新数据库表中的数据DELETE - 从数据库表中删除数据INSERT INTO">
<meta property="og:type" content="article">
<meta property="og:title" content="Day7-MySQL 知识、数据库、数据表">
<meta property="og:url" content="http://blog.uiste.com/20160829-1.html">
<meta property="og:site_name" content="uiste">
<meta property="og:description" content="SQL DML 和 DDL可以把 SQL 分为两个部分：数据操作语言 (DML) 和 数据定义语言 (DDL)。SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。查询和更新指令构成了 SQL 的 DML 部分：SELECT - 从数据库表中获取数据UPDATE - 更新数据库表中的数据DELETE - 从数据库表中删除数据INSERT INTO">
<meta property="og:updated_time" content="2016-08-31T05:57:14.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Day7-MySQL 知识、数据库、数据表">
<meta name="twitter:description" content="SQL DML 和 DDL可以把 SQL 分为两个部分：数据操作语言 (DML) 和 数据定义语言 (DDL)。SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。查询和更新指令构成了 SQL 的 DML 部分：SELECT - 从数据库表中获取数据UPDATE - 更新数据库表中的数据DELETE - 从数据库表中删除数据INSERT INTO">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://blog.uiste.com/20160829-1.html"/>

  <title>
 Day7-MySQL 知识、数据库、数据表 | uiste 
</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?b58b87af012832b8ae35e33bb45b44fa";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">uiste</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">uiste个人博客</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      

            <li class="menu-item menu-item-search"> <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High</a> </li>

    </ul>
  

 
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
   
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                Day7-MySQL 知识、数据库、数据表
              
            
          </h1>
        

        <div class="post-meta">
        
  <span id="busuanzi_container_page_pv">阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>

          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-08-29T17:18:51+08:00" content="2016-08-29">
              2016-08-29
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/MySQL/" itemprop="url" rel="index">
                    <span itemprop="name">MySQL</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160829-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160829-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="SQL-DML-和-DDL"><a href="#SQL-DML-和-DDL" class="headerlink" title="SQL DML 和 DDL"></a>SQL DML 和 DDL</h3><p>可以把 SQL 分为两个部分：数据操作语言 (DML) 和 数据定义语言 (DDL)。<br>SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。<br>查询和更新指令构成了 SQL 的 DML 部分：<br>SELECT - 从数据库表中获取数据<br>UPDATE - 更新数据库表中的数据<br>DELETE - 从数据库表中删除数据<br>INSERT INTO - 向数据库表中插入数据<br>SQL 的数据定义语言 (DDL) 部分使我们有能力创建或删除表格。我们也可以定义索引（键），规定表之间的链接，以及施加表间的约束。<br>SQL 中最重要的 DDL 语句:<br>CREATE DATABASE - 创建新数据库<br>ALTER DATABASE - 修改数据库<br>CREATE TABLE - 创建新表<br>ALTER TABLE - 变更（改变）数据库表<br>DROP TABLE - 删除表<br>CREATE INDEX - 创建索引（搜索键）<br>DROP INDEX - 删除索引</p>
<h3 id="数据库发展史"><a href="#数据库发展史" class="headerlink" title="数据库发展史"></a>数据库发展史</h3><p>文件系统-》层次模型-》网状模型-》关系型数据库<br>优点：解决了导航的问题、数据完整性得到了解决</p>
<p>缺点：多表查询效率低<br>解决方法：使用NOSQL（非关系型数据库，Redis、mongodb等等），在数据库中按照键值对来存储。</p>
<h3 id="SQL"><a href="#SQL" class="headerlink" title="SQL"></a>SQL</h3><p>结构化查询语言，标准SQL，也叫SQL-92</p>
<p>access    微软    SQL<br>SQL Server    微软    T-SQL<br>Oracle    甲骨文    PL/SQL<br>MySQL    被甲骨文收购    MySQL</p>
<h3 id="windows-服务开启"><a href="#windows-服务开启" class="headerlink" title="windows 服务开启"></a>windows 服务开启</h3><p>win+R    services.msc 打开服务面板<br>net start    查看已经开启的服务</p>
<p><code>net start mysql</code>      启动服务<br><code>net stop mysql</code>     关闭服务</p>
<h3 id="MySQL客户端连接服务器"><a href="#MySQL客户端连接服务器" class="headerlink" title="MySQL客户端连接服务器"></a>MySQL客户端连接服务器</h3><p>安装文件目录下的：<code>MySQL 5.5 Command Line Client</code><br>navicat 客户端<br>PHPmyadmin 客户端<br>设置环境变量后：<code>mysql  -hlocalhost  -uroot -proot  -P3306</code>   (如果MySQL服务器在本地，IP地址可以省略；如果MySQL服务器用的是3306端口，-P也是可以省略的)</p>
<h3 id="退出登录"><a href="#退出登录" class="headerlink" title="退出登录"></a>退出登录</h3><p>exit;<br>quit;<br>\q;<br>MySQL中的命令后面要加分号，windows命令行的命令后面不用加分号</p>
<h3 id="数据库基本概念"><a href="#数据库基本概念" class="headerlink" title="数据库基本概念"></a>数据库基本概念</h3><p>数据库：数据库中存放的是表，一个数据库中可以存放多个表<br>表：用来存放数据<br>关系：两个表的公共字段<br>行：记录，也是实体<br>列：字段，也是属性<br>表结构    行    列<br>表数据    记录    字段<br>面向对象    实体    属性</p>
<blockquote>
<p>表的冗余只能减少，不能杜绝</p>
</blockquote>
<p>数据完整性：正确的数据类型，准确的数据范围</p>
<h3 id="数据库操作"><a href="#数据库操作" class="headerlink" title="数据库操作"></a>数据库操作</h3><h4 id="连接数据库"><a href="#连接数据库" class="headerlink" title="连接数据库"></a>连接数据库</h4><p><code>Mac xampp ：$ /Applications/XAMPP/xamppfiles/bin/mysql -hlocalhost -uroot -p -P3306</code><br>密码初始为空；</p>
<h4 id="创建数据库"><a href="#创建数据库" class="headerlink" title="创建数据库"></a>创建数据库</h4><p>语法：<code>create database [if not exists] 数据名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create database if not exists uiste;</div><div class="line">Query OK, 1 row affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>问题一：</p>
<blockquote>
<p>如果创建的数据库已存在，就会报错<br>解决：创建数据库的时候判断一下数据库是否存在，如果不存在再创建</p>
</blockquote>
<p>问题二：</p>
<blockquote>
<p>如果数据库名是关键字和特殊字符要报错<br>解决：在特殊字符、关键字行加上反引号<br><code>create database `$@%`;</code></p>
</blockquote>
<p><strong>为了创建数据库时万无一失，我们可以在所有的数据库名上加上反引号</strong></p>
<h4 id="MySQL数据库的目录"><a href="#MySQL数据库的目录" class="headerlink" title="MySQL数据库的目录"></a>MySQL数据库的目录</h4><p>数据库文件保存的路径在安装MySQL的时候就配置好。<br>也可以在<code>my.ini</code>配置文件中更改</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">#Path to the database root</div><div class="line">datadir = &quot;F:/wamp/mysql/Data/&quot;</div></pre></td></tr></table></figure>
<p>Mac xampp 数据库文件夹：<br><code>/Applications /XAMPP/xamppfiles/var/mysql</code></p>
<p>创建一个数据库，就对应一个文件夹，在文件夹中有一个db.opt文件。在此文件中设置数据库的字符集和校对集</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">default-character-set = utf8</div><div class="line">default-collation = utf_general_ci</div></pre></td></tr></table></figure>
<h4 id="显示所有数据库【show-databases】"><a href="#显示所有数据库【show-databases】" class="headerlink" title="显示所有数据库【show databases】"></a>显示所有数据库【show databases】</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show databases;</div><div class="line">+--------------------+</div><div class="line">| Database           |</div><div class="line">+--------------------+</div><div class="line">| information_schema |</div><div class="line">| cdcol              |</div><div class="line">| dedecms            |</div><div class="line">| mysql              |</div><div class="line">| performance_schema |</div><div class="line">| phpmyadmin         |</div><div class="line">| test               |</div><div class="line">| uiste              |</div><div class="line">+--------------------+</div><div class="line">8 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>information_schema：存储了MySQL服务器管理数据库的信息。比如：数据库名、表、字段名、数据类型及访问权限等等<br>performance_schema：MySQL5.5新增的表，用来保存数据库服务器性能的参数<br>mysql：MySQL系统数据库，保存的登录用户名，密码，以及每个用户的权限等等<br>test：给用户学习和测试的数据库。</p>
</blockquote>
<h4 id="删除数据库"><a href="#删除数据库" class="headerlink" title="删除数据库"></a>删除数据库</h4><p>语法：<code>drop database [if exists] `数据库名` </code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop database if exists uiste;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div><div class="line"></div><div class="line">mysql&gt; show databases;</div><div class="line">+--------------------+</div><div class="line">| Database           |</div><div class="line">+--------------------+</div><div class="line">| information_schema |</div><div class="line">| cdcol              |</div><div class="line">| dedecms            |</div><div class="line">| mysql              |</div><div class="line">| performance_schema |</div><div class="line">| phpmyadmin         |</div><div class="line">| test               |</div><div class="line">+--------------------+</div><div class="line">7 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<p>问题：</p>
<blockquote>
<p>如果删除的数据库不存在，会报错<br>解决：删除之前判断一下，如果存在就删除</p>
</blockquote>
<h4 id="显示创建数据库的语句"><a href="#显示创建数据库的语句" class="headerlink" title="显示创建数据库的语句"></a>显示创建数据库的语句</h4><p>语法：<code>show create database 数据库名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create database uiste;</div><div class="line">+----------+------------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                  |</div><div class="line">+----------+------------------------------------------------------------------+</div><div class="line">| uiste    | CREATE DATABASE `uiste` /*!40100 DEFAULT CHARACTER SET latin1 */ |</div><div class="line">+----------+------------------------------------------------------------------+</div><div class="line">1 row in set (0.00 sec)</div></pre></td></tr></table></figure>
<h4 id="修改数据库字符编码"><a href="#修改数据库字符编码" class="headerlink" title="修改数据库字符编码"></a>修改数据库字符编码</h4><p>语法：<code>alter database `数据库名` charset=字符编码</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter database uiste charset=utf8;</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; show create database uiste;</div><div class="line">+----------+----------------------------------------------------------------+</div><div class="line">| Database | Create Database                                                |</div><div class="line">+----------+----------------------------------------------------------------+</div><div class="line">| uiste    | CREATE DATABASE `uiste` /*!40100 DEFAULT CHARACTER SET utf8 */ |</div><div class="line">+----------+----------------------------------------------------------------+</div><div class="line">1 row in set (0.00 sec)</div></pre></td></tr></table></figure>
<h4 id="选择数据库"><a href="#选择数据库" class="headerlink" title="选择数据库"></a>选择数据库</h4><p>语法：<code>use `数据库名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; use uiste;</div><div class="line">Database changed</div></pre></td></tr></table></figure>
<h3 id="表的操作"><a href="#表的操作" class="headerlink" title="表的操作"></a>表的操作</h3><h4 id="创建表"><a href="#创建表" class="headerlink" title="创建表"></a>创建表</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">create table [if not exists] `表名`(</div><div class="line">	`字段名` 数据类型 [null|not null] [default] [auto_increment] [primary key] [comment],</div><div class="line">	字段名 数据类型 …</div><div class="line">)[engine=存储引擎] [charset=字符编码]</div><div class="line"></div><div class="line">null|not null		是否为空</div><div class="line">default:			默认值</div><div class="line">auto_increment	自动增长</div><div class="line">primary key		主键</div><div class="line">comment:			备注</div><div class="line">engine				引擎</div></pre></td></tr></table></figure>
<blockquote>
<p>表名和字段名如果用了关键字，要用反引号引起来。</p>
</blockquote>
<p>创建简单的表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table student(</div><div class="line">    -&gt; id int not null auto_increment primary key comment &apos;学生编号&apos;,</div><div class="line">    -&gt; name varchar(10) not null comment &apos;学生姓名&apos;,</div><div class="line">    -&gt; sex char(4) default &apos;男&apos;);</div><div class="line">Query OK, 0 rows affected (0.03 sec)</div></pre></td></tr></table></figure>
<p>创建复杂的表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table `goods`(</div><div class="line">    -&gt; `id` int auto_increment primary key comment &apos;商品编号&apos;,</div><div class="line">    -&gt; `name` varchar(20) not null comment &apos;商品名称&apos;,</div><div class="line">    -&gt; `add` varchar(100) not null default &apos;地址不详&apos; comment &apos;商品产地&apos;</div><div class="line">    -&gt; )engine=innodb charset=utf8 comment &apos;商品表&apos;;</div><div class="line">Query OK, 0 rows affected (0.05 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>create table 数据库名.表名，用于给指定的数据库创建表</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table uiste.teacher(</div><div class="line">    -&gt; id int auto_increment primary key,</div><div class="line">    -&gt; name varchar(16) not null);</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div></pre></td></tr></table></figure>
<h4 id="数据表文件"><a href="#数据表文件" class="headerlink" title="数据表文件"></a>数据表文件</h4><ul>
<li>一个数据库对应一个文件夹</li>
<li>一个表对应一个或多个文件夹<br>引擎是myisam，一个表对应三个文件(不支持外键约束)<blockquote>
<p>t1.frm  表结构<br>t1.MYD  表数据<br>t1.MYI  表索引<br>引擎是innodb,一个表对应一个表结构文件</p>
</blockquote>
</li>
</ul>
<blockquote>
<p>所有的innodb引擎的数据统一的存放在data\ibdata1文件中。如果数据量很大，MySQL会自动的创建ibdata2，ibdata3，…，目的就是为了便于管理。</p>
</blockquote>
<p>constraint：约束</p>
<h4 id="显示所有表"><a href="#显示所有表" class="headerlink" title="显示所有表"></a>显示所有表</h4><p>语法：<code>show tables;</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show tables;</div><div class="line">+-----------------+</div><div class="line">| Tables_in_uiste |</div><div class="line">+-----------------+</div><div class="line">| student         |</div><div class="line">| teacher         |</div><div class="line">+-----------------+</div><div class="line">2 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<h4 id="显示创建表的语句"><a href="#显示创建表的语句" class="headerlink" title="显示创建表的语句"></a>显示创建表的语句</h4><p>语法：<code>show create table `表名` </code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create table student;</div><div class="line">+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</div><div class="line">| Table   | Create Table                                                                                                                                                                                                                           |</div><div class="line">+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</div><div class="line">| student | CREATE TABLE `student` (</div><div class="line">  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT &apos;学生编号&apos;,</div><div class="line">  `name` varchar(10) NOT NULL COMMENT &apos;学生姓名&apos;,</div><div class="line">  `sex` char(4) DEFAULT &apos;男&apos;,</div><div class="line">  PRIMARY KEY (`id`)</div><div class="line">) ENGINE=InnoDB DEFAULT CHARSET=utf8          |</div><div class="line">+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+</div><div class="line">1 row in set (0.01 sec)</div></pre></td></tr></table></figure>
<p><strong>将两个字段竖着排列</strong><br>语法：<code>show create table `表名`\G</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show create table student \G</div><div class="line">*************************** 1. row ***************************</div><div class="line">       Table: student</div><div class="line">Create Table: CREATE TABLE `student` (</div><div class="line">  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT &apos;学生编号&apos;,</div><div class="line">  `name` varchar(10) NOT NULL COMMENT &apos;学生姓名&apos;,</div><div class="line">  `sex` char(4) DEFAULT &apos;男&apos;,</div><div class="line">  PRIMARY KEY (`id`)</div><div class="line">) ENGINE=InnoDB DEFAULT CHARSET=utf8</div><div class="line">1 row in set (0.00 sec)</div></pre></td></tr></table></figure>
<h4 id="查看表结构"><a href="#查看表结构" class="headerlink" title="查看表结构"></a>查看表结构</h4><p>语法：<code>desc[ribe] `表名` </code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line">mysql&gt; desc student;</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">| Field | Type        | Null | Key | Default | Extra          |</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">| id    | int(11)     | NO   | PRI | NULL    | auto_increment |</div><div class="line">| name  | varchar(10) | NO   |     | NULL    |                |</div><div class="line">| sex   | char(4)     | YES  |     | 男      |                |</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">3 rows in set (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; describe student;</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">| Field | Type        | Null | Key | Default | Extra          |</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">| id    | int(11)     | NO   | PRI | NULL    | auto_increment |</div><div class="line">| name  | varchar(10) | NO   |     | NULL    |                |</div><div class="line">| sex   | char(4)     | YES  |     | 男      |                |</div><div class="line">+-------+-------------+------+-----+---------+----------------+</div><div class="line">3 rows in set (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>desc 是 describe 的简写</p>
</blockquote>
<h4 id="复制表"><a href="#复制表" class="headerlink" title="复制表"></a>复制表</h4><p>语法一：<code>create table 新表 select 字段 from 旧表</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table student2 select * from student;</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<blockquote>
<p>特点：不能复制父表的键，能够复制父表的数据</p>
</blockquote>
<p>语法二：create table 新表 like 旧表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; create table student3 like student;</div><div class="line">Query OK, 0 rows affected (0.03 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>特点：只能复制表结构，不能复制表数据</p>
</blockquote>
<h4 id="删除表"><a href="#删除表" class="headerlink" title="删除表"></a>删除表</h4><p>语法：<code>drop table [if exists] 表1，表2,…</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop table if exists student2;</div><div class="line">Query OK, 0 rows affected (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>如果删除一个不存在的表就会报错，删除的时候可以判断一下，存在就删除</p>
</blockquote>
<p>可以一次删除多个表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; drop tables if exists student2,student3;</div><div class="line">Query OK, 0 rows affected (0.01 sec)</div></pre></td></tr></table></figure>
<h4 id="修改表"><a href="#修改表" class="headerlink" title="修改表"></a>修改表</h4><p>语法：<code>alter table 表名</code></p>
<h5 id="添加字段"><a href="#添加字段" class="headerlink" title="添加字段"></a>添加字段</h5><p>语法：<code>alter table 表名add [column] 字段名 数据类型 [位置]</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student add age int(8) after name;</div><div class="line">Query OK, 0 rows affected (0.20 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<blockquote>
<p>默认字段放在表的最后<br>添加 <code>first</code> 字段放在表的第一位<br>添加 <code>after name</code> 放在 name 字段之后</p>
</blockquote>
<h5 id="删除字段"><a href="#删除字段" class="headerlink" title="删除字段"></a>删除字段</h5><p>语法：<code>alter table 表 drop [column] 字段名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student drop age;</div><div class="line">Query OK, 0 rows affected (0.05 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<h5 id="修改字段-改名"><a href="#修改字段-改名" class="headerlink" title="修改字段(改名)"></a>修改字段(改名)</h5><p>语法：<code>alter table 表 change [column] 原字段名 新字段名 数据类型 …</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student change sex age int(8);</div><div class="line">Query OK, 0 rows affected (0.32 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<p>同时修改多个字段</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student change age myage int(8) comment &apos;我的学生年龄&apos;,change name myname varchar(16) not null comment &apos;我的学生姓名&apos;;</div><div class="line">Query OK, 0 rows affected (0.04 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<h5 id="修改字段（不改名）"><a href="#修改字段（不改名）" class="headerlink" title="修改字段（不改名）"></a>修改字段（不改名）</h5><p>语法：<code>alter table 表 modify 字段名 字段属性…</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student modify myage int(3) not null default 18 comment &apos;如果你没 有设置年龄，我就是永远的18岁&apos;;</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<blockquote>
<p>只更改字段属性</p>
</blockquote>
<h5 id="修改引擎"><a href="#修改引擎" class="headerlink" title="修改引擎"></a>修改引擎</h5><p>语法：<code>alter table 表名 engine=引擎名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student engine=myisam;</div><div class="line">Query OK, 0 rows affected (0.18 sec)</div><div class="line">Records: 0  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<h5 id="修改表名"><a href="#修改表名" class="headerlink" title="修改表名"></a>修改表名</h5><p>语法：<code>alter table 表名 rename to 新表名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; alter table student rename to student2;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<h5 id="将表移动到其他数据库"><a href="#将表移动到其他数据库" class="headerlink" title="将表移动到其他数据库"></a>将表移动到其他数据库</h5><p>语法：<code>alter table 表名 rename to 数据库名.数据表名</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">alter table student2 rename to test.student3;</div><div class="line">Query OK, 0 rows affected (0.01 sec)</div></pre></td></tr></table></figure>
<h4 id="数据操作"><a href="#数据操作" class="headerlink" title="数据操作"></a>数据操作</h4><h5 id="插入数据"><a href="#插入数据" class="headerlink" title="插入数据"></a>插入数据</h5><p>语法：<code>insert into 表名 (字段名, 字段名,…) values (值1, 值1,…)</code></p>
<ul>
<li>插入一条数据</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student (id,name,age) values (1,&apos;小兰&apos;,25);</div><div class="line">Query OK, 1 row affected (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<ol>
<li>插入字段名的顺序和数据库字段名的顺序可以不一致</li>
<li>插入值的个数、顺序必须和插入字段名的个数、顺序要一致</li>
</ol>
</blockquote>
<ul>
<li>简写方式：</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (2,&apos;杜总&apos;,24);</div><div class="line">Query OK, 1 row affected (0.02 sec)</div></pre></td></tr></table></figure>
<blockquote>
<ol>
<li>自动增长列，可以直接插入null。</li>
</ol>
</blockquote>
<ul>
<li>插入默认值和空值</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,&apos;张宝&apos;,null);</div><div class="line">Query OK, 1 row affected (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>自动增长列，可以直接插入null<br>default关键字用来插入默认值，null用来插入空值</p>
</blockquote>
<ul>
<li>插入多条数据</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,&apos;小张&apos;,18),(null,&apos;圆哥&apos;,18);</div><div class="line">Query OK, 2 rows affected (0.00 sec)</div><div class="line">Records: 2  Duplicates: 0  Warnings: 0</div></pre></td></tr></table></figure>
<ul>
<li>使用insert…set…插入数据</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student set name=&apos;赵哥&apos;,age=54;</div><div class="line">Query OK, 1 row affected (0.01 sec)</div></pre></td></tr></table></figure>
<h5 id="更新数据"><a href="#更新数据" class="headerlink" title="更新数据"></a>更新数据</h5><p>语法：<code>update 表名 set 字段=值 [where 条件] [order by 排序] [limit 限制]</code></p>
<ul>
<li>将id为三的年龄改为30</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; update student set age=30 where id=3;</div><div class="line">Query OK, 1 row affected (0.01 sec)</div><div class="line">Rows matched: 1  Changed: 1  Warnings: 0</div></pre></td></tr></table></figure>
<blockquote>
<p>需要更改多个内容的时候将需要变更的内容用逗号连接；</p>
</blockquote>
<ul>
<li>将id 从大到小数前3名的学生年龄改为16岁</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">mysql&gt; update student set age=16 order by id desc limit 3;</div><div class="line">Query OK, 3 rows affected (0.01 sec)</div><div class="line">Rows matched: 3  Changed: 3  Warnings: 0</div></pre></td></tr></table></figure>
<h5 id="删除数据"><a href="#删除数据" class="headerlink" title="删除数据"></a>删除数据</h5><p>语法：<code>delete from 表名 [where 条件] [order by 排序] [limit 限制]</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; delete from student where id=9;</div><div class="line">Query OK, 1 row affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>删除id &lt;=7;</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; delete from student where id&lt;=7;</div><div class="line">Query OK, 7 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>清空表</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; delete from student;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>清空表：truncate table 表名</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; truncate table student;</div><div class="line">Query OK, 0 rows affected (0.02 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>delete from 表和truncate table 表区别？<br>delete from 表：遍历表记录，一条一条的删除<br>truncate table：将原表销毁，再创建一个同结构的新表。就清空表而言，这种方法效率高。</p>
</blockquote>
<h4 id="字符集"><a href="#字符集" class="headerlink" title="字符集"></a>字符集</h4><p>字符集：字符在保存和传输时对应的二进制编码集合</p>
<h5 id="数据存储时使用字符集"><a href="#数据存储时使用字符集" class="headerlink" title="数据存储时使用字符集"></a>数据存储时使用字符集</h5><p>MySQL可以在服务器上、数据库、表、字段上设置字符集</p>
<blockquote>
<p>脚下留心：一般在数据库和表上设置字符集，每个字段设置字符集太麻烦。</p>
</blockquote>
<h5 id="数据传输时使用字符集"><a href="#数据传输时使用字符集" class="headerlink" title="数据传输时使用字符集"></a>数据传输时使用字符集</h5><p>发现：在插入数据的时候，如果有中文会报错</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt; insert into student values (null,&apos;李白&apos;);</div><div class="line">ERROR 1366 (HY000):Incorrect string value :&apos;\xC0\xEE\xB0\xD7&apos; for column &apos;name&apos;</div></pre></td></tr></table></figure>
<blockquote>
<p>分析：客户端编码和服务器连接层编码必须一致，如果不一致就会报错。</p>
</blockquote>
<p>查看客户端的字符编码</p>
<blockquote>
<p>客户端发送某种编码给服务器，由客户端决定<br>客户端编码不能改变<br>客户端右键——属性——选项——可以查看当前客户端的字符编码（GBK）</p>
</blockquote>
<p>查看服务器的字符编码</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show variables like &apos;character_set_%&apos;;</div><div class="line">+--------------------------+------------------------------------------------+</div><div class="line">| Variable_name            | Value                                          |</div><div class="line">+--------------------------+------------------------------------------------+</div><div class="line">| character_set_client     | utf8                                           |</div><div class="line">| character_set_connection | utf8                                           |</div><div class="line">| character_set_database   | utf8                                           |</div><div class="line">| character_set_filesystem | binary                                         |</div><div class="line">| character_set_results    | utf8                                           |</div><div class="line">| character_set_server     | latin1                                         |</div><div class="line">| character_set_system     | utf8                                           |</div><div class="line">| character_sets_dir       | /Applications/XAMPP/xamppfiles/share/charsets/ |</div><div class="line">+--------------------------+------------------------------------------------+</div><div class="line">8 rows in set (0.01 sec)</div></pre></td></tr></table></figure>
<blockquote>
<p>解决：只要将服务器的编码设置成gbk就可以解决</p>
</blockquote>
<p>测试：插入中文<br>查询：插入中文</p>
<p>原因：结果返回的是utf8，客户端用gbk来解析，所以乱码<br>解决：服务器按gbk返回即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">mysql&gt;set character_set_results=gbk;</div><div class="line">Query OK, 0 rows affected (0.00 sec)</div></pre></td></tr></table></figure>
<p>查询：结果</p>
<blockquote>
<p>总结：客户端编码、<code>character_set_client</code>、<code>character_set_results</code>三个编码的值一致即可操作中文。<br>多学一招：我们只要设置<code>“set names 字符编码”</code>，就可以更改<code>character_set_client</code>、<code>character_set_results</code>的值。</p>
</blockquote>
<h5 id="查看所有字符集"><a href="#查看所有字符集" class="headerlink" title="查看所有字符集"></a>查看所有字符集</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div></pre></td><td class="code"><pre><div class="line"></div><div class="line">mysql&gt; show character set;</div><div class="line">+----------+-----------------------------+---------------------+--------+</div><div class="line">| Charset  | Description                 | Default collation   | Maxlen |</div><div class="line">+----------+-----------------------------+---------------------+--------+</div><div class="line">| big5     | Big5 Traditional Chinese    | big5_chinese_ci     |      2 |</div><div class="line">| dec8     | DEC West European           | dec8_swedish_ci     |      1 |</div><div class="line">| cp850    | DOS West European           | cp850_general_ci    |      1 |</div><div class="line">| hp8      | HP West European            | hp8_english_ci      |      1 |</div><div class="line">| koi8r    | KOI8-R Relcom Russian       | koi8r_general_ci    |      1 |</div><div class="line">| latin1   | cp1252 West European        | latin1_swedish_ci   |      1 |</div><div class="line">| latin2   | ISO 8859-2 Central European | latin2_general_ci   |      1 |</div><div class="line">| swe7     | 7bit Swedish                | swe7_swedish_ci     |      1 |</div><div class="line">| ascii    | US ASCII                    | ascii_general_ci    |      1 |</div><div class="line">| ujis     | EUC-JP Japanese             | ujis_japanese_ci    |      3 |</div><div class="line">| sjis     | Shift-JIS Japanese          | sjis_japanese_ci    |      2 |</div><div class="line">| hebrew   | ISO 8859-8 Hebrew           | hebrew_general_ci   |      1 |</div><div class="line">| tis620   | TIS620 Thai                 | tis620_thai_ci      |      1 |</div><div class="line">| euckr    | EUC-KR Korean               | euckr_korean_ci     |      2 |</div><div class="line">| koi8u    | KOI8-U Ukrainian            | koi8u_general_ci    |      1 |</div><div class="line">| gb2312   | GB2312 Simplified Chinese   | gb2312_chinese_ci   |      2 |</div><div class="line">| greek    | ISO 8859-7 Greek            | greek_general_ci    |      1 |</div><div class="line">| cp1250   | Windows Central European    | cp1250_general_ci   |      1 |</div><div class="line">| gbk      | GBK Simplified Chinese      | gbk_chinese_ci      |      2 |</div><div class="line">| latin5   | ISO 8859-9 Turkish          | latin5_turkish_ci   |      1 |</div><div class="line">| armscii8 | ARMSCII-8 Armenian          | armscii8_general_ci |      1 |</div><div class="line">| utf8     | UTF-8 Unicode               | utf8_general_ci     |      3 |</div><div class="line">| ucs2     | UCS-2 Unicode               | ucs2_general_ci     |      2 |</div><div class="line">| cp866    | DOS Russian                 | cp866_general_ci    |      1 |</div><div class="line">| keybcs2  | DOS Kamenicky Czech-Slovak  | keybcs2_general_ci  |      1 |</div><div class="line">| macce    | Mac Central European        | macce_general_ci    |      1 |</div><div class="line">| macroman | Mac West European           | macroman_general_ci |      1 |</div><div class="line">| cp852    | DOS Central European        | cp852_general_ci    |      1 |</div><div class="line">| latin7   | ISO 8859-13 Baltic          | latin7_general_ci   |      1 |</div><div class="line">| utf8mb4  | UTF-8 Unicode               | utf8mb4_general_ci  |      4 |</div><div class="line">| cp1251   | Windows Cyrillic            | cp1251_general_ci   |      1 |</div><div class="line">| utf16    | UTF-16 Unicode              | utf16_general_ci    |      4 |</div><div class="line">| utf16le  | UTF-16LE Unicode            | utf16le_general_ci  |      4 |</div><div class="line">| cp1256   | Windows Arabic              | cp1256_general_ci   |      1 |</div><div class="line">| cp1257   | Windows Baltic              | cp1257_general_ci   |      1 |</div><div class="line">| utf32    | UTF-32 Unicode              | utf32_general_ci    |      4 |</div><div class="line">| binary   | Binary pseudo charset       | binary              |      1 |</div><div class="line">| geostd8  | GEOSTD8 Georgian            | geostd8_general_ci  |      1 |</div><div class="line">| cp932    | SJIS for Windows Japanese   | cp932_japanese_ci   |      2 |</div><div class="line">| eucjpms  | UJIS for Windows Japanese   | eucjpms_japanese_ci |      3 |</div><div class="line">+----------+-----------------------------+---------------------+--------+</div><div class="line">40 rows in set (0.01 sec)</div></pre></td></tr></table></figure>
<h4 id="校对集"><a href="#校对集" class="headerlink" title="校对集"></a>校对集</h4><p>在某种特定的字符集下，字符和字符的大小关系。比如a和B的大小关系：如果不区分大小写a<b；如果区分大小写a>B。<br>脚下留心：不同的校对集他们的比较结果是不一样的<br>语法：<code>collate=校对集</code><br>创建两个表</b；如果区分大小写a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">create table stu1(</div><div class="line">name varchar(20)</div><div class="line">)charset=utf8 collate=utf8_general_ci;</div><div class="line"></div><div class="line">create table stu2(</div><div class="line">name varchar(20)</div><div class="line">)charset=utf8 collate=utf8_bin;</div><div class="line">–插入测试数据</div><div class="line">insert into stu1 values (&apos;a&apos;),(&apos;B&apos;);</div><div class="line">insert into stu2 values (&apos;a&apos;),(&apos;B&apos;);</div></pre></td></tr></table></figure>
<p>测试</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">mysql&gt; select * from stu1 order by name;</div><div class="line">+------+</div><div class="line">| name |</div><div class="line">+------+</div><div class="line">| a    |</div><div class="line">| B    |</div><div class="line">+------+</div><div class="line">2 rows in set (0.01 sec)</div><div class="line"></div><div class="line">mysql&gt; select * from stu2 order by name;</div><div class="line">+------+</div><div class="line">| name |</div><div class="line">+------+</div><div class="line">| B    |</div><div class="line">| a    |</div><div class="line">+------+</div><div class="line">2 rows in set (0.00 sec)</div></pre></td></tr></table></figure>
<p>校对集的规则：<br>1、_bin:按二进制编码进行比较<br>2、_ci:不区分大小写进行比较</p>
<p><strong>查看所有的校对集</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">mysql&gt; show collation;</div><div class="line">+--------------------------+----------+-----+---------+----------+---------+</div><div class="line">| Collation                | Charset  | Id  | Default | Compiled | Sortlen |</div><div class="line">+--------------------------+----------+-----+---------+----------+---------+</div><div class="line">| big5_chinese_ci          | big5     |   1 | Yes     | Yes      |       1 |</div><div class="line">| big5_bin                 | big5     |  84 |         | Yes      |       1 |</div><div class="line">| dec8_swedish_ci          | dec8     |   3 | Yes     | Yes      |       1 |</div><div class="line">| dec8_bin                 | dec8     |  69 |         | Yes      |       1 |</div><div class="line"></div><div class="line">此处省略200多行...</div></pre></td></tr></table></figure>
<p><a href="http://oamgu5mjf.bkt.clouddn.com/%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C.docx" target="_blank" rel="external">常用操作命令文件下载</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/MySQL巩固与加强/" rel="tag">#MySQL巩固与加强</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20160828-1.html" rel="next" title="php数组比较-usort">
                <i class="fa fa-chevron-left"></i> php数组比较-usort
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20160830-1.html" rel="prev" title="Day8-MySQL 数据类型、列属性、数据完整性">
                Day8-MySQL 数据类型、列属性、数据完整性 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type = "text/javascript" src = "//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57838eee5e2620d0" async = "async" ></script>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="20160829-1.html"
           data-title="Day7-MySQL 知识、数据库、数据表" data-url="http://blog.uiste.com/20160829-1.html">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="uiste" />
          <p class="site-author-name" itemprop="name">uiste</p>
          <p class="site-description motion-element" itemprop="description">Stay Hungry，Stay Foolish</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/uiste" target="_blank" rel="external nofollow" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/uiste" target="_blank" rel="external nofollow" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/uiste" target="_blank" rel="external nofollow" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.uiste.com" title="uiste 个人博客" target="_blank">uiste 个人博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://uiste.github.io" title="uiste 技术博客" target="_blank">uiste 技术博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#SQL-DML-和-DDL"><span class="nav-number">1.</span> <span class="nav-text">SQL DML 和 DDL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据库发展史"><span class="nav-number">2.</span> <span class="nav-text">数据库发展史</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQL"><span class="nav-number">3.</span> <span class="nav-text">SQL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#windows-服务开启"><span class="nav-number">4.</span> <span class="nav-text">windows 服务开启</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#MySQL客户端连接服务器"><span class="nav-number">5.</span> <span class="nav-text">MySQL客户端连接服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#退出登录"><span class="nav-number">6.</span> <span class="nav-text">退出登录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据库基本概念"><span class="nav-number">7.</span> <span class="nav-text">数据库基本概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据库操作"><span class="nav-number">8.</span> <span class="nav-text">数据库操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#连接数据库"><span class="nav-number">8.1.</span> <span class="nav-text">连接数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建数据库"><span class="nav-number">8.2.</span> <span class="nav-text">创建数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#MySQL数据库的目录"><span class="nav-number">8.3.</span> <span class="nav-text">MySQL数据库的目录</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#显示所有数据库【show-databases】"><span class="nav-number">8.4.</span> <span class="nav-text">显示所有数据库【show databases】</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除数据库"><span class="nav-number">8.5.</span> <span class="nav-text">删除数据库</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#显示创建数据库的语句"><span class="nav-number">8.6.</span> <span class="nav-text">显示创建数据库的语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改数据库字符编码"><span class="nav-number">8.7.</span> <span class="nav-text">修改数据库字符编码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#选择数据库"><span class="nav-number">8.8.</span> <span class="nav-text">选择数据库</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#表的操作"><span class="nav-number">9.</span> <span class="nav-text">表的操作</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#创建表"><span class="nav-number">9.1.</span> <span class="nav-text">创建表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据表文件"><span class="nav-number">9.2.</span> <span class="nav-text">数据表文件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#显示所有表"><span class="nav-number">9.3.</span> <span class="nav-text">显示所有表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#显示创建表的语句"><span class="nav-number">9.4.</span> <span class="nav-text">显示创建表的语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#查看表结构"><span class="nav-number">9.5.</span> <span class="nav-text">查看表结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#复制表"><span class="nav-number">9.6.</span> <span class="nav-text">复制表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#删除表"><span class="nav-number">9.7.</span> <span class="nav-text">删除表</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#修改表"><span class="nav-number">9.8.</span> <span class="nav-text">修改表</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#添加字段"><span class="nav-number">9.8.1.</span> <span class="nav-text">添加字段</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除字段"><span class="nav-number">9.8.2.</span> <span class="nav-text">删除字段</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#修改字段-改名"><span class="nav-number">9.8.3.</span> <span class="nav-text">修改字段(改名)</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#修改字段（不改名）"><span class="nav-number">9.8.4.</span> <span class="nav-text">修改字段（不改名）</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#修改引擎"><span class="nav-number">9.8.5.</span> <span class="nav-text">修改引擎</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#修改表名"><span class="nav-number">9.8.6.</span> <span class="nav-text">修改表名</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#将表移动到其他数据库"><span class="nav-number">9.8.7.</span> <span class="nav-text">将表移动到其他数据库</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数据操作"><span class="nav-number">9.9.</span> <span class="nav-text">数据操作</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#插入数据"><span class="nav-number">9.9.1.</span> <span class="nav-text">插入数据</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#更新数据"><span class="nav-number">9.9.2.</span> <span class="nav-text">更新数据</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#删除数据"><span class="nav-number">9.9.3.</span> <span class="nav-text">删除数据</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#字符集"><span class="nav-number">9.10.</span> <span class="nav-text">字符集</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#数据存储时使用字符集"><span class="nav-number">9.10.1.</span> <span class="nav-text">数据存储时使用字符集</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#数据传输时使用字符集"><span class="nav-number">9.10.2.</span> <span class="nav-text">数据传输时使用字符集</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#查看所有字符集"><span class="nav-number">9.10.3.</span> <span class="nav-text">查看所有字符集</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#校对集"><span class="nav-number">9.11.</span> <span class="nav-text">校对集</span></a></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">uiste</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io" rel="external nofollow">Hexo</a> 趋势动力
</div>
<!--站长统计-->
<script src="https://s95.cnzz.com/z_stat.php?id=1259894121&web_id=1259894121" language="JavaScript"></script>
<!--<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div> -->
&nbsp;&nbsp;&nbsp;本站总点击 <span id="busuanzi_value_site_pv"></span> 次
&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;您是第 <span id="busuanzi_value_site_uv"></span> 位访客

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"uiste"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

</body>
</html>
